package TankFrst;
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;

public class NetClient{
    TankClient tc;
//    private static int UDP_PORT_START = 2223;
    private int udpPort;//UDP端口号
    String IP;//定义IP地址

    DatagramSocket ds = null;

    public int getUdpPort() {
        return udpPort;
    }

    public void setUdpPort(int udpPort) {
        this.udpPort = udpPort;
    }

    public NetClient(TankClient tc) {
        this.tc = tc;

    }

    public void connect(String IP, int port) {

        this.IP=IP;

        try {
            ds = new DatagramSocket(udpPort);//初始化坦克
        } catch (SocketException e) {
            e.printStackTrace();
        }
        Socket s = null;
        try {
            s = new Socket(IP, port);
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            dos.writeInt(udpPort);
            //读出Id号码
            DataInputStream dis = new DataInputStream(s.getInputStream());
            int id = dis.readInt();
            tc.myTank.id = id;

            if (id%2==1) tc.myTank.setGood(false);
            else tc.myTank.setGood(true);
            System.out.println("Connected to server! and server give me a ID:" + id);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (s != null) {
                try {
                    s.close();
                    s = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        TankNewMsg msg = new TankNewMsg(tc.myTank);//创建一个信息对象，发送信息对象
        send(msg);

        new Thread(new UDPRecvThread()).start();//启动UDP线程
    }


    public void send(Msg msg) {
        msg.send(ds, IP, TankServer.UDP_PORT);//构造方法来发送信息
    }

    private class UDPRecvThread implements Runnable {

        byte[] buf = new byte[1024];

        @Override
        public void run() {
            //接受数据包

            while (ds != null) {
                DatagramPacket dp = new DatagramPacket(buf, buf.length);
                try {
                    ds.receive(dp);
                    parse(dp);
                    System.out.println("a pocket received from server!");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
//解析服务端发送的消息
        private void parse(DatagramPacket dp) {
            ByteInputStream bais=new ByteInputStream(buf,0,dp.getLength());
            DataInputStream dis=new DataInputStream(bais);
            int msgType= 0;
            try {
                msgType = dis.readInt();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Msg msg=null;
            switch (msgType){
                //传输坦克进来时的消息
                case Msg.TAKE_NEW_MSG:
                    msg=new TankNewMsg(NetClient.this.tc);
                    msg.parse(dis);
                    break;
                case Msg.TAKE_MOVE_MSG://传递坦克移动的时候的消息
                    msg=new TankMoveMsg(NetClient.this.tc);
                    msg.parse(dis);
                    break;
                case Msg.MISSILE_NEW_MSG://传递子弹的时候的消息
                    msg=new MissileNewMsg(NetClient.this.tc);
                    msg.parse(dis);
                    break;
                case Msg.TANK_DEAD_MSG://传递坦克死亡的时候的消息
                    msg=new TankDeadMsg(NetClient.this.tc);
                    msg.parse(dis);
                    break;
                case Msg.MISSILE_DEAD_MSG://传递子弹死亡的时候的消息
                    msg=new MissileDeadMsg(NetClient.this.tc);
                    msg.parse(dis);
                    break;
            }
        }
    }
}
